/**
 * cateGory: 饼图
 */
import { mergeOption, setSeries } from '../util'
export default {
  name: 'pie',
  option(param, extraOpts) {
    return mergeOption(
      param,
      {
        legend: {
          top: 'bottom',
          itemWidth: 10,
          itemGap: 25,
          itemHeight: 10,
          padding: [0, 5],
          icon: 'rect',
          textStyle: {
            color: '#999'
          }
        },
        tooltip: {
          trigger: 'item'
        },
        title: {
          text: param.series.length === 1 ? sumTotal(param.series) : '',
          left: 'center',
          top: '40%',
          textStyle: {
            fontSize: 30,
            fontWeight: 'bold'
          },
          subtext: '',
          subtextStyle: {
            fontSize: 15,
            fontWeight: 'bold',
            color: '#999'
          }
        },
        color: ['#ff791a', '#ffc09e', '#7b5ccc', '#a28ddb', '#e4def5', '#ffe8bd'],
        series: setSeries({ series: param.series }, extraOpts)
      },
      extraOpts
    )
  }
}

function sumTotal(series) {
  let total = 0
  const arr = series[0].data.map((item) => item.value)
  arr.forEach((data) => {
    total += data
  })
  return total.toFixed(2)
}
